import 'package:flutter/material.dart';
import "package:get/get.dart";

import '../../../../common/my_icon.dart';
import '../../../../config/global_config.dart';
import '../../../../utils/screenutil/fit.dart';
import '../ctrl/lift_details_ctrl.dart';

class InfoList extends StatelessWidget {
  String str = "assets/images/";
  double _top;
  InfoList({double top = 0.0}) : this._top = top;

  final _ldc = Get.find<LiftDetailsCtrl>();

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => Container(
        margin: EdgeInsets.only(top: _top),
        child: _ldc.offline.value == 0
            ? Wrap(
                children: [
                  createContainer(
                    "运行状态",
                    1,
                    value: matching(
                      _ldc.latest.value.state.status,
                      {"0": "停止", "1": "上行", "2": "下行"},
                    ),
                  ),
                  createContainer(
                    "楼层",
                    2,
                    value: _ldc.latest.value.state.louCeng,
                  ),
                  createContainer(
                    "是否有电动车",
                    3,
                    value: matching(
                      _ldc.latest.value.alarm.electromobile,
                      {"0": "无", "1": "有"},
                    ),
                  ),
                  createContainer(
                    "速度",
                    4,
                    value: _ldc.latest.value.state.speed,
                  ),
                  createContainer(
                    "开关门",
                    5,
                    value: matching(
                      _ldc.latest.value.state.door,
                      {"0": "关门", "1": "开门"},
                    ),
                  ),
                  createContainer(
                    "是否有人",
                    6,
                    value: matching(
                      _ldc.latest.value.state.people,
                      {"0": "无", "1": "有"},
                    ),
                  ),
                  createContainer(
                    "温度",
                    7,
                    value: connect(_ldc.latest.value.state.temp, '℃'),
                  ),
                  createContainer(
                    "湿度",
                    8,
                    value: _ldc.latest.value.state.hum,
                  ),
                  createContainer(
                    "振幅",
                    9,
                    value: _ldc.latest.value.state.amplitude,
                  ),
                  createContainer(
                    "前后偏移",
                    10,
                    value: connect(_ldc.latest.value.state.degreeFb, '°'),
                  ),
                  createContainer(
                    "左右偏移",
                    11,
                    value: connect(_ldc.latest.value.state.degreeLr, '°'),
                  ),
                  createContainer(
                    "空间",
                    12,
                    value: matching(
                      _ldc.latest.value.state.dsdx,
                      {"0": "地下", "1": "地上"},
                    ),
                  ),
                ],
              )
            : Container(
                width: 640.f,
                color: Colors.white,
                padding: EdgeInsets.symmetric(vertical: 40.f),
                child: Column(
                  children: [
                    Icon(
                      MyIcon.disconnect,
                      size: 100.f,
                      color: GlobalConfig.assistColor,
                    ),
                    SizedBox(height: 10.f),
                    Text(
                      "监控设备已离线",
                      style: TextStyle(
                        color: GlobalConfig.assistColor,
                        fontSize: 22.f,
                      ),
                    ),
                  ],
                ),
              ),
      ),
    );
  }

  /// 创建列表模块
  /// * [name] String|名称|必填
  /// * [i] int|下标|必填
  /// * [value] dynamic|内容值|选填
  Widget createContainer(String name, int i, {dynamic value = '未知'}) {
    return Container(
      padding: EdgeInsets.symmetric(
        vertical: 15.f,
        horizontal: 10.f,
      ),
      decoration: BoxDecoration(
        color: Colors.white,
        border: Border(
          bottom: i > 9
              ? BorderSide(color: Colors.white)
              : BorderSide(
                  color: GlobalConfig.borderColor,
                ),
          right: BorderSide(
            color: GlobalConfig.borderColor,
          ),
        ),
      ),
      width: 213.f,
      constraints: BoxConstraints(minHeight: 130.f),
      alignment: Alignment.center,
      child: Container(
        // color: Colors.blueGrey,
        child: Row(
          children: [
            Container(
              width: 60.f,
              margin: EdgeInsets.only(right: 10.f),
              child: Image.asset(
                "${str}${i}.png",
                fit: BoxFit.cover,
              ),
            ),
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  "$name",
                  style: TextStyle(
                    fontSize: 20.f,
                    color: GlobalConfig.assistColor,
                  ),
                ),
                Container(
                  width: 100.f,
                  margin: EdgeInsets.only(top: 10.f),
                  child: Text(
                    "$value",
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 28.f,
                    ),
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }

  /// 连接字符串
  /// * [value] dynamic|值|必填
  /// * [unit] String|单位符号|必填
  String connect(dynamic value, String unit) => "${value}${unit}";

  /// 字符匹配
  /// * [value] dynamic|值|必填
  /// * [source] Map|匹配的数据源|必填
  String matching(dynamic value, Map source) => source[value];
}
